package com.sisgesbyte.dao.sistema;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sisgesbyte.dao.beanDao.LocalDetRequest;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.entity.sistema.LocalesListaEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantenimientoLocalDaoImpl extends AbstractFacade<LocalesListaEntity> implements MantenimientoLocalDao{

	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}
	
	public MantenimientoLocalDaoImpl(){
		super(LocalesListaEntity.class);
	}
	
	@Override
	public List<LocalesListaEntity> listarLocales() {
		
			StringBuilder jpql = new StringBuilder();
		
			jpql.append("select l.id,l.cod_local,l.descripcion,l.direccion,l.representante, l.telefono,l.ip_local,l.empresa_id,e.razon_social,l.active ");
			jpql.append("from sistema.locales l ");
			jpql.append("inner join sistema.empresa e on l.empresa_id=e.id order by l.id desc");
			
			Query query = em.createNativeQuery(jpql.toString(), LocalesListaEntity.class);
			
			List<LocalesListaEntity> ls = query.getResultList();

		return ls;
	}

	@Override
	public GenericEntity guardarLocal(LocalDetRequest local) {
		
		StringBuilder jpql = new StringBuilder();
		
		jpql.append("select * from sistema.sp_registrar_locales(:in_empresaid,:in_direccion,:in_telefono,:in_representante,:in_active,:in_iplocal,:in_codlocal,:in_descripcion)");
		
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_empresaid", local.getCodEmpresa());
			query.setParameter("in_direccion", local.getDireccion());
			query.setParameter("in_telefono", local.getTelefono());
			query.setParameter("in_representante", local.getRepresentante());
			query.setParameter("in_active", local.getActive());
			query.setParameter("in_iplocal", local.getIpLocal());
			query.setParameter("in_codlocal", local.getCodLocal());
			query.setParameter("in_descripcion", local.getDescripcion());
		
		return (GenericEntity)query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarLocal(LocalDetRequest local) {
		
		StringBuilder jpql = new StringBuilder();
		
		jpql.append("select * from sistema.sp_actualizar_locales(:in_localid,:in_empresaid,:in_direccion,:in_telefono,:in_representante,:in_active,:in_iplocal,:in_codlocal,:in_descripcion)");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_localid", local.getIdLocal());
			query.setParameter("in_empresaid", local.getCodEmpresa());
			query.setParameter("in_direccion", local.getDireccion());
			query.setParameter("in_telefono", local.getTelefono());
			query.setParameter("in_representante", local.getRepresentante());
			query.setParameter("in_active", local.getActive());
			query.setParameter("in_iplocal", local.getIpLocal());
			query.setParameter("in_codlocal", local.getCodLocal());
			query.setParameter("in_descripcion", local.getDescripcion());
		
		return (GenericEntity)query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarLocal(int idLocal) {
		

		StringBuilder jpql = new StringBuilder();
		
		jpql.append("select * from sistema.sp_eliminar_locales(:in_localid)");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_localid", idLocal);

		return (GenericEntity)query.getSingleResult();
	}

}
